package com.it.myd.service.main.module.world;

import com.it.myd.bean.box.world.*;
import com.it.myd.bean.main.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class PlayerServiceImpl implements PlayerService{
    @Autowired
    private ItemService itemService;
    private List<Player> playerList = new ArrayList<>();
    @Override
    public void setPlayerList(List<Player> playerList){
        this.playerList = playerList;
    }
    @Override
    public List<Player> getPlayerList() {
        return playerList;
    }
    @Override
    public void playerMove(){
        if(playerList != null && playerList.size() > 0){
            for (Player player: playerList) {
                Position position = player.getPlayer_position();
                if(position!= null && position.getToLeft() >=0 && position.getToTop() >=0 && position.getToLeft()!=position.getInLeft()&&position.getToTop()!=position.getInTop()){
                    double inLeft = position.getInLeft()+position.getGoLeft();
                    double inTop = position.getInTop()+position.getGoTop();
                    position.setInLeft(inLeft);
                    position.setInTop(inTop);
                    double toLeft = position.getToLeft();
                    double toTop = position.getToTop();
                    double speed = getPlayerAbility(player.getPlayer_info().getUser_id(),null).getSpeed();

                    // 计算距离 AB
                    double AB = Math.sqrt(Math.pow(toLeft - inLeft, 2) + Math.pow(toTop - inTop, 2));

                    // 计算角度 theta
                    double theta = Math.atan2(toTop - inTop, toLeft - inLeft);

                    double goLeft = 0;
                    double goTop = 0;
                    // 判断移动距离是否大于起点和终点之间的距离
                    if (speed >= AB) {
                        goLeft = toLeft - inLeft;
                        goTop = toTop - inTop;

                        position.setToLeft(-1);
                        position.setToTop(-1);
                    }else{
                        goLeft = speed * Math.cos(theta);
                        goTop = speed * Math.sin(theta);
                    }

                    position.setGoLeft(goLeft);
                    position.setGoTop(goTop);

                }else{
                    double inLeft = position.getInLeft()+position.getGoLeft();
                    double inTop = position.getInTop()+position.getGoTop();
                    position.setInLeft(inLeft);
                    position.setInTop(inTop);

                    position.setGoLeft(0);
                    position.setGoTop(0);
                }
            }
        }
    }

    @Override
    public void playerCreate(){
        if(playerList != null && playerList.size() > 0){
            for (Player player: playerList) {
                if(player.getPlayer_state()== 4 ){
                    itemService.playerCreate(player.getPlayer_info());
                }
            }
        }
    }

    @Override
    public void addPlayer(User user) {
        Player player = new Player();
        player.setPlayer_info(user);
        player.setPlayer_state(0);
        Ability ability = new Ability();
        ability.setStrength(2);
        ability.setSpeed(10);
        player.setAbility(ability);
        Position position = new Position();
        position.setToTop(-1);
        position.setToLeft(-1);
        position.setGoTop(0);
        position.setGoLeft(0);
        player.setPlayer_position(position);
        this.playerList.add(player);
    }

    @Override
    public void changerState(User user, int state) {
        if(playerList != null && playerList.size() > 0){
            for (Player player: playerList) {
                if(user.getUser_id()==player.getPlayer_info().getUser_id()){
                    player.setPlayer_state(state);
                    break;
                }
            }
        }
    }

    @Override
    public void playerMove(User user, double toLeft, double toTop) {
        if(playerList != null && playerList.size() > 0){
            for (Player player: playerList) {
                if(user.getUser_id()==player.getPlayer_info().getUser_id()){
                    player.getPlayer_position().setToTop(toTop);
                    player.getPlayer_position().setToLeft(toLeft);
                    break;
                }
            }
        }
    }

    @Override
    public void outPlayer(Resource resource) {
        if(playerList != null && playerList.size() > 0){
            for (Player player: playerList) {
                if(resource.getUseUser().getUser_id()==player.getPlayer_info().getUser_id()){
                    player.setPlayer_state(1);
                    player.getPlayer_position().setInTop(Double.parseDouble(resource.getRes_Top())+50);
                    player.getPlayer_position().setInLeft(Double.parseDouble(resource.getRes_Left())+50);
                    break;
                }
            }
        }
    }

    @Override
    public int getPlayerSh(User user,MyObject obj) {
        int user_sh = 0;
        Ability ability = getPlayerAbility(user.getUser_id(),obj);
        if(ability!=null){
            user_sh = ability.getStrength();
        }
        return user_sh;
    }

    @Override
    public void stopPlayer(User user) {
        if(playerList != null && playerList.size() > 0){
            for (Player player: playerList) {
                if(user.getUser_id()==player.getPlayer_info().getUser_id()){
                    player.getPlayer_position().setGoLeft(0);
                    player.getPlayer_position().setGoTop(0);
                    player.getPlayer_position().setToLeft(-1);
                    player.getPlayer_position().setToTop(-1);
                    break;
                }
            }
        }
    }

    @Override
    public Ability getPlayerAbility(int userid,MyObject obj){
        Player player = new Player();
        Ability ability = new Ability();
        if(playerList != null && playerList.size() > 0){
            for (Player p: playerList) {
                if(p.getPlayer_info().getUser_id() == userid){
                    player = p;
                    ability = p.getAbility();
                    break;
                }
            }
        }
        if(player != null){
            List<Item> useruseItems = itemService.getuserUseItemList().get(String.valueOf(userid));
            if(useruseItems != null){
                for (Item i : useruseItems){
                    ability = ability.addAbility(i.getAllAbility(obj));
                    if(i.getState().equals("3")){
                        itemService.removeuserUseItem(player.getPlayer_info(),i.getCode());
                    }
                }
                return ability;
            }
        }
        return ability;
    }

}
